/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package managedBean;

import bean.business.curriculumBean;
import bean.business.semesterBean;
import entities.Curriculum;
import entities.Semester;
import java.util.LinkedList;
import java.util.List;
import javax.ejb.EJB;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;
import javax.faces.context.FacesContext;

/**
 *
 * @author HoangHai
 */
@ManagedBean
@RequestScoped
public class manageSemester {

    @EJB
    private curriculumBean curriculumBean;
    @EJB
    private semesterBean semesterBean;
    private Semester sem = new Semester();
    private Semester sem1 = new Semester();
    private List<Semester> list;
    private int curID;

    public int getCurID() {
        return curID;
    }

    public void setCurID(int curID) {
        this.curID = curID;
    }
    /** Creates a new instance of manageSemester */
    public manageSemester() {
    }

    public Semester getSem() {
        return sem;
    }

    public void setSem(Semester sem) {
        this.sem = sem;
    }

    public Semester getSem1() {
        return sem1;
    }

    public List<Semester> getList() {
        list = semesterBean.allSemester();
        return list;
    }

    public void setList(List<Semester> list) {
        this.list = list;
    }

    public void setSem1(Semester sem1) {
        this.sem1 = sem1;
    }

    public void insert() {
        if (checkName()) {
            Curriculum c = curriculumBean.findCurByID(curID);
        sem1.setCuID(c);
        semesterBean.addSemester(sem1);
        FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_INFO, "Sucessful", "Sucessful"));
        }
    }

    public void update() {
        Curriculum cu = curriculumBean.findCurByID(curID);
        sem.setCuID(cu);
        semesterBean.updateSemester(sem);
        FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_INFO, "Sucessful", "Sucessful"));
    }

    public void delete() {
        semesterBean.delSemester(sem.getSeID());
    }
    public boolean checkName(){
        List<Semester> ses = new LinkedList<Semester>();
        ses = semesterBean.allSemester();
        for (Semester semester : ses) {
            if (semester.getSeName().equals(sem1.getSeName())) {
                FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR, "Error", "Name had been Already"));
                return false;
            }
        }
        return true;
    }
}
